package com.muqiuhan.osmanthus.utils

import org.springframework.context.ApplicationContext
import org.springframework.context.ApplicationContextAware

/** Spring's context tool class */
object ContextUtils extends ApplicationContextAware:
  private var _context: Option[ApplicationContext] = None
  inline def context: Option[ApplicationContext]   = _context

  inline override def setApplicationContext(applicationContext: ApplicationContext): Unit =
    _context = Some(applicationContext)

  inline def getBeanByName(name: String): Option[?]          = _context map { _.getBean(name) }
  inline def getBeanByClass(clazz: Class[AnyRef]): Option[?] = _context map { _.getBean(clazz) }

  inline def getByTypeAndName[T](clazz: Class[T], name: String): Option[T] =
    _context map { _.getBeansOfType(clazz).get(name) }

end ContextUtils
